﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace CueMounter.DokanStructure
{
	class DokanMirrorDirectory : DokanDirectory
	{
		public string TargetPath { get; set; }

		public DokanMirrorDirectory(string targetPath, string name = null)
		{
			if(name == null)
			{
				Name = Path.GetFileName(targetPath);
			}
			else
			{
				Name = name;
			}
			TargetPath = targetPath;
		}

		public override DokanFile[] Files
		{
			get
			{
				List<DokanMirrorFile> files = new List<DokanMirrorFile>();
				foreach(string file in Directory.GetFiles(TargetPath))
				{
					files.Add(new DokanMirrorFile(file));
				}
				return files.ToArray();
			}
		}

		public override DokanFile GetFile(string name)
		{
			string target = Path.Combine(TargetPath, name);
			if(File.Exists(target))
			{
				return new DokanMirrorFile(target);
			}
			return null;
		}

		public override void CreateFile(string name)
		{
			using(File.Create(Path.Combine(TargetPath, name)))
			{
			}
		}

		public override void DeleteFile(string name)
		{
			File.Delete(Path.Combine(TargetPath, name));
		}

		public override DokanDirectory[] Directories
		{
			get
			{
				List<DokanMirrorDirectory> directories = new List<DokanMirrorDirectory>();
				foreach(string file in Directory.GetDirectories(TargetPath))
				{
					directories.Add(new DokanMirrorDirectory(file));
				}
				return directories.ToArray();
			}
		}

		public override DokanDirectory GetDirectory(string name)
		{
			string target = Path.Combine(TargetPath, name);
			if(Directory.Exists(target))
			{
				return new DokanMirrorDirectory(target);
			}
			return null;
		}

		public override void CreateDirectory(string name)
		{
			Directory.CreateDirectory(Path.Combine(TargetPath, name));
		}

		public override void DeleteDirectory(string name)
		{
			Directory.Delete(Path.Combine(TargetPath, name), true);
		}

		public override DokanFileDates Dates
		{
			get
			{
				return new DokanFileDates(TargetPath);
			}
			set
			{
				Directory.SetCreationTime(TargetPath, value.CreateTime);
				Directory.SetLastAccessTime(TargetPath, value.AccessTime);
				Directory.SetLastWriteTime(TargetPath, value.ModifyTime);
			}
		}

		public override void MoveFile(string[] oldPath, string[] newPath, bool replace)
		{
			string oldTargetPath = Path.Combine(TargetPath, string.Join(Path.DirectorySeparatorChar.ToString(), oldPath));
			string newTargetPath = Path.Combine(TargetPath, string.Join(Path.DirectorySeparatorChar.ToString(), newPath));
			if(replace && File.Exists(newTargetPath))
			{
				File.Delete(newTargetPath);
			}
			File.Move(oldTargetPath, newTargetPath);
		}

		public override void MoveDirectory(string[] oldPath, string[] newPath, bool replace)
		{
			string oldTargetPath = Path.Combine(TargetPath, string.Join(Path.DirectorySeparatorChar.ToString(), oldPath));
			string newTargetPath = Path.Combine(TargetPath, string.Join(Path.DirectorySeparatorChar.ToString(), newPath));
			if(replace && Directory.Exists(newTargetPath))
			{
				Directory.Delete(newTargetPath);
			}
			Directory.Move(oldTargetPath, newTargetPath);
		}
	}
}
